import java.util.*;

public class Solution {


    public int[] twoSum(int[] nums, int target) {

    }

   public int[] answer(int[] nums, int target) {
        int[] arr = new int[2];
        HashMap<Integer, Integer> map = new HashMap<>();
        for(int i=0; i<nums.length; i++){
            if(map.containsKey(target-nums[i])){
                arr[0] = i;
                arr[1] = map.get(target-nums[i]);
                return arr;
            }
            map.put(nums[i], i);
        }
        return arr;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();

        //  测试次数
        int testTime = 5000;
        // 构建题目的测试条件
        int maxSize = 10000; // 随机数组的长度 0~100
        int maxValue = 10000; // 随机数组的范围 -10000 ~ 10000
        int target = (int)((maxSize + 1) * Math.random()) - (int)(maxSize * Math.random());
        // 开始测试
        for (int i = 0; i < testTime; i++) {
            // 初始化两类需要测试的数据（随机）
            int[] arr1 = generateRandomArray(maxSize, maxValue);
            int[] arr2 = copyArray(arr1);
            // 通过两种方法对测试数据进行测试
            int[] arr3 = solution.answer(arr1, target);
            int[] arr4 = solution.twoSum(arr2, target);
            // 比较测试后的结果
            if (!isEqual(arr1, arr2)) {
                // 打印两种方法的测试结果
                System.out.println("结果错误！测试数据如下：");
                System.out.println("nums: " + arr1);
                System.out.println("target: " + target);
                System.out.println("正确结果为: " + arr3.toString());
                System.out.println("你的结果为: " + arr4.toString());
                return;
            }
        }
        System.out.println("结果正确!");
    }

    public static int[] generateRandomArray(int maxSize, int maxValue){
        // 等概率生成一个数组长度为 0 ~ maxSize 的数组
        int len = (int)((maxSize + 1) * Math.random()) >= 2 ?  (int)((maxSize + 1) * Math.random()): 2;
        int[] arr = new int[len];
        for(int i=0; i<arr.length; i++){
            arr[i] = (int)((maxSize + 1) * Math.random()) - (int)(maxSize * Math.random());
        }
        return arr;
    }

    public static int[] copyArray(int[] arr){
        if(arr == null){
            return null;
        }
        int len = arr.length;
        int[] copyArr = new int[len];
        for(int i=0; i<len; i++){
            copyArr[i] = arr[i];
        }
        return copyArr;
    }

    public static boolean isEqual(int[] arr1, int[] arr2){
        if(arr1 == null || arr2 == null){
            return false;
        }
        for(int i=0; i<arr1.length; i++){
            if(arr1[i] != arr2[i]){
                return false;
            }
        }
        return true;
    }
}